package com.shinhye.mobilelottery.ui.manager;

import java.util.Observable;
import java.util.Observer;

import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shinhye.mobilelottery.ConstantValue;
import com.shinhye.mobilelottery.R;
import com.shinhye.mobilelottery.ui.BaseUI;

public class BottomManager implements Observer{
	private BottomManager(){}
	private static BottomManager instance = new BottomManager();
	public static BottomManager getInstance(){
		return instance;
	}
	
	private final String TAG = "BottomManager";
	
	private RelativeLayout bottomMenuContainer;
	private LinearLayout commonBottom;
	private LinearLayout playBottom;
	private TextView playBottomNotice;
	private ImageButton cleanButton;
	private ImageButton addButton;
	

	public void init(Activity activity) {
		bottomMenuContainer = (RelativeLayout) activity.findViewById(R.id.ii_bottom);
		commonBottom = (LinearLayout) activity.findViewById(R.id.ii_bottom_common);
		playBottom = (LinearLayout) activity.findViewById(R.id.ii_bottom_game);

		playBottomNotice = (TextView) activity.findViewById(R.id.ii_bottom_game_choose_notice);
		cleanButton = (ImageButton) activity.findViewById(R.id.ii_bottom_game_choose_clean);
		addButton = (ImageButton) activity.findViewById(R.id.ii_bottom_game_choose_ok);
		
		setListener();
	}

	private void setListener() {
		cleanButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Log.i(TAG, "点击清空按钮");
				BaseUI currUI = MiddleManager.getInstance().getCurrUI();
				if(currUI instanceof PlayGame) {
					((PlayGame) currUI).clear();
				}
			}
		});
		
		addButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Log.i(TAG, "点击选好按钮");
				BaseUI currUI = MiddleManager.getInstance().getCurrUI();
				if(currUI instanceof PlayGame) {
					((PlayGame) currUI).done();
				}
			}
		});
	}
	

	/**
	 * 转换到通用导航
	 */
	public void showCommonBottom() {
		if (bottomMenuContainer.getVisibility() == View.GONE || bottomMenuContainer.getVisibility() == View.INVISIBLE) {
			bottomMenuContainer.setVisibility(View.VISIBLE);
		}
		commonBottom.setVisibility(View.VISIBLE);
		playBottom.setVisibility(View.INVISIBLE);
	}

	/**
	 * 转换到购彩
	 */
	public void showGameBottom() {
		if (bottomMenuContainer.getVisibility() == View.GONE || bottomMenuContainer.getVisibility() == View.INVISIBLE) {
			bottomMenuContainer.setVisibility(View.VISIBLE);
		}
		commonBottom.setVisibility(View.INVISIBLE);
		playBottom.setVisibility(View.VISIBLE);
	}

	/**
	 * 改变底部导航容器显示情况
	 * @param type 使用View.INVISIBLE, View.VISIBLE, View.GONE
	 */
	public void changeBottomVisiblity(int type) {
		if (bottomMenuContainer.getVisibility() != type)
			bottomMenuContainer.setVisibility(type);
	}

	/**
	 * 设置玩法底部提示信息
	 * 
	 * @param notice
	 */
	public void changeGameBottomNotice(String notice) {
		playBottomNotice.setText(notice);
	}

	@Override
	public void update(Observable observable, Object data) {
		if(data!=null&&StringUtils.isNumeric(data.toString()))
		{
			int id=Integer.parseInt(data.toString());
			switch(id)
			{
			case ConstantValue.VIEW_FIRST:
			case ConstantValue.VIEW_HALL:
			case ConstantValue.VIEW_USERLOGIN:
				showCommonBottom();
				break;
			case ConstantValue.VIEW_PLAYSSQ:
			case ConstantValue.VIEW_SECOND:
				showGameBottom();
				break;
			case ConstantValue.VIEW_SHOPPING:
			case ConstantValue.VIEW_PREBET:
				changeBottomVisiblity(View.GONE);
				break;
			}
		}
	}
}
